/**
 * @projectName CollegeEmploymentNetwork
 * @package com.kzj.CollegeEmploymentNetwork.shiro.controller
 * @className com.kzj.CollegeEmploymentNetwork.shiro.controller.LoginController
 * @copyright Copyright 2025 Thunisoft, Inc All rights reserved.
 */
package com.kzj.CollegeEmploymentNetwork.shiro.controller;

import com.kzj.CollegeEmploymentNetwork.shiro.pojo.dto.LoginDTO;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * LoginController
 * @description
 * @author KZJ
 * @date 2025/1/23 20:51
 * @version TODO
 */
@Controller
@RequestMapping("/login")
@Slf4j
public class LoginController {
    @GetMapping
    public String html(){
        return "login";
    }
    @PostMapping
    @ResponseBody
    public Object login(@RequestBody LoginDTO loginDTO){
        Subject user = SecurityUtils.getSubject();
        try{
            user.login(new UsernamePasswordToken(loginDTO.getUsername(),loginDTO.getPassword()));
        }catch (IncorrectCredentialsException e){
            throw new RuntimeException("密码错误");
        }
        System.out.println(user.hasRole("admin"));
        return loginDTO;
    }
}